<template>
  <div @scroll="gundong()">
    <header>
      <div class="commonwidth fle">
        <div class="logo"></div>
        <ul v-if="show == false">
          <li class="c"><a href="#/">首页</a></li>
          <li
            class="showInfo"
            v-for="(item, index) in arr"
            :key="index"
            @mouseover="over(index)"
            @mouseout="out"
            @click="clk(item.name,item.id)"
          >
            <a href="javascript:;">{{ item.name }}</a>
            <div class="layer" :class="{ shou: num == index }">
              <ul>
                <li v-for="(item, index) in item.children" :key="index">
                  <img :src="item.picture" alt="" />
                  <span>{{ item.name }}</span>
                </li>
              </ul>
            </div>
          </li>
        </ul>
        <div class="search">
          <input type="text" placeholder="搜一搜" />
          <span>购物车</span>
            
          <div class="cart" @mouseover="huacar" @mouseout="chucar">
         
            <a href="#/cart" class="curr">
              <span>{{ $store.getters.getcount }}</span>
            </a>
            <div class="layer" :class="{ xianshi123: bool }">
              <div class="list">
                <div class="item" v-for="(item, index) in cart" :key="index">
                  <a
                    aria-current="page"
                    :href="'#/product/' + item.id"
                    class="router-link-active router-link-exact-active"
                    ><img :src="item.picture" alt="" />
                    <div class="center">
                      <p class="name ellipsis-2">{{ item.name }}</p>
                      <p class="attr ellipsis">{{ item.attrsText }}</p>
                    </div>
                    <div class="right">
                      <p class="price">¥{{ item.price }}</p>
                      <p class="count">x{{ item.count }}</p>
                    </div></a
                  ><i class="iconfont icon-close-new"></i>
                </div>
              </div>
              <div class="foot">
                <div class="total">
                  <p>共{{ $store.getters.getcount }}件商品</p>
                  <p>¥{{ $store.getters.getmoney }}</p>
                </div>
                <button
                  @click="gocart"
                  class="xtx-button ellipsis middle plain"
                >
                  去购物车结算
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </header>
    <div
      class="app-header-sticky"
      v-if="show"
      :class="{ xinashi: show == true }"
    >
      <div class="commonwidth fle">
        <div class="logo"></div>
        <ul>
          <li class="c"><a href="#/">首页</a></li>
          <li
            class="showInfo"
            v-for="(item, index) in arr"
            :key="index"
            @mouseover="over1(index)"
            @mouseout="out"
          >
            <a href="javascript:;">{{ item.name }}</a>
            <div class="layer" :class="{ shou: nume == index }">
              <ul>
                <li v-for="(item, index) in item.children" :key="index">
                  <img :src="item.picture" alt="" />
                  <span>{{ item.name }}</span>
                </li>
              </ul>
            </div>
          </li>
        </ul>
        <div class="ppa">
          <span>品牌</span>
          <span>专题</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      str: "",
      show: false,
      num: -1,
      nume: -1,
      id: this.$route.query.id,
      arr: [],
      ind: -1,
      bool: false,
      cart: [],
    };
  },
  methods: {
    over(index) {
      this.num = index;
    },
    over1(index) {
      this.nume = index;
    },
    out() {
      this.num = -1;
      this.nume = -1;
    },
    huaru(index) {
      this.ind = index;
    },
    huachu() {
      this.ind = -1;
    },
    huacar() {
      this.bool = true;
      this.getcar();
    },
    chucar() {
      this.bool = false;
    },

    gundong() {
      var scrollTop = document.documentElement.scrollTop;
      if (scrollTop > 80) {
        this.show = true;
      } else {
        this.show = false;
      }
    },
    clk(title,id) {
      // console.log(item.id);
      this.$router.push({path:'/category',query:{id:id}});
      console.log(id); 
      this.$route.meta.title='首页>'+title
    },
    getcar() {
      if (this.$store.state.login.logo.token != undefined) {
        this.axios
          .get("https://apipc-xiaotuxian-front.itheima.net/member/cart", {
            headers: {
              Authorization: "Bearer " + this.$store.state.login.logo.token,
            },
          })
          .then((res) => {
            this.cart = res.data.result;
          });
      }
    },
    gocart() {
      this.$router.push("/cart");
    },
  },
  mounted() {
    window.addEventListener("scroll", this.gundong);
    this.axios
      .get("https://apipc-xiaotuxian-front.itheima.net/home/category/head")
      .then((res) => {
        if (res.data.msg == "操作成功") {
          this.arr = res.data.result;
        }
      });
  },
};
</script>
<style  lang="scss" scoped>
.xianshi123 {
  opacity: 1 !important;
  transform: none !important;
}
 ul li a {
  line-height: 32px;
  font-size: 16px;
  height: 32px;
  display: inline-block;
}
.ppa span{
  line-height: 32px;
  font-size: 16px;
  height: 32px;
  display: inline-block;
}
.cart {
  width: 50px;
  position: relative;
  z-index: 600;
  .curr {
    height: 36px;
    text-align: center;
    position: relative;
    display: block;
  }
  .layer {
    &::before {
      content: "";
      position: absolute;
      right: 14px;
      top: -10px;
      width: 20px;
      height: 20px;
      background: #fff;
      transform: scaleX(0.6) rotate(45deg);
      box-shadow: -3px -3px 5px rgb(0 0 0 / 10%);
    }
    opacity: 0;
    transition: all 0.4s 0.2s;
    transform: translateY(-200px) scaleY(0);
    width: 400px;
    height: 400px;
    position: absolute;
    top: 50px;
    right: 0;
    box-shadow: 0 0 10px rgb(0 0 0 / 20%);
    background: #fff;
    border-radius: 4px;
    padding-top: 10px;
    .list {
      height: 310px;
      overflow: auto;
      padding: 0 10px;
      .item {
        border-bottom: 1px solid #f5f5f5;
        padding: 10px 0;
        position: relative;
        a {
          display: flex;
          align-items: center;
          img {
            height: 80px;
            width: 80px;
          }
          .center {
            padding: 0 10px;
            width: 200px;
            .name {
              font-size: 16px;
              word-break: break-all;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .attr {
              color: #999;
              padding-top: 5px;
              text-overflow: ellipsis;
              overflow: hidden;
              white-space: nowrap;
            }
          }
          .right {
            width: 100px;
            padding-right: 20px;
            text-align: center;
            .price {
              font-size: 16px;
              color: #cf4444;
            }
            .count {
              color: #999;
              margin-top: 5px;
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  .foot {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 70px;
    width: 100%;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    background: #f8f8f8;
    align-items: center;
  }
}
.shou {
  opacity: 1;
  height: 132px;
  display: block;
}
</style>